package cn.jcby.app.lebu.service.impl;

import cn.jcby.app.lebu.controller.req.BasicListReq;
import cn.jcby.app.lebu.controller.res.DictRes;
import cn.jcby.app.lebu.controller.res.MyTagListRes;
import cn.jcby.app.lebu.controller.res.sub.UserTagRes;
import cn.jcby.app.lebu.dao.DictDao;
import cn.jcby.app.lebu.dao.UserTabDao;
import cn.jcby.app.lebu.entity.DictEntity;
import cn.jcby.app.lebu.entity.UserTabEntity;
import cn.jcby.app.lebu.service.DictService;
import cn.jcby.app.lebu.service.UserTabService;
import cn.jcby.app.lebu.utils.Constants;
import cn.jcby.app.lebu.utils.PageUtils;
import cn.jcby.app.lebu.utils.UserInfoUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("userTabService")
public class UserTabServiceImpl extends ServiceImpl<UserTabDao, UserTabEntity> implements UserTabService {

    @Autowired
    private UserTabDao userTabDao;
    @Autowired
    private DictService dictService;
    @Autowired
    private DictDao dictDao;
    @Override
    public PageUtils queryPage(BasicListReq params) {
        IPage<UserTabEntity> page = this.page(
                new Page<>(params.getPage(),params.getLimit()),
                Wrappers.<UserTabEntity>lambdaQuery()
        );

        return new PageUtils(page);
    }
    @Override
    public void add(UserTabEntity userTabReq){
        UserTabEntity userTab = new UserTabEntity() ;
        save(userTab) ;
    }

    @Override
    public void updateBy(UserTabEntity userTabReq){
        UserTabEntity userTab = new UserTabEntity() ;
        updateById(userTab) ;
    }

    @Override
    //@CacheEvict(key = "'userId-tags:'+#userId",cacheNames = {"userTab"})
    public void updateTagesByUserId(String userId, List<String> tagList) {
        //清理之前标签
        userTabDao.deleteByUserId(userId);
        if(tagList!=null && tagList.size()>0){
            List<UserTabEntity> saveTagList = new ArrayList<>();
            tagList.forEach(tag->{
                UserTabEntity userTab = new UserTabEntity() ;
                userTab.setUserId(userId);
                userTab.setCode(tag);
                saveTagList.add(userTab) ;
            });
            saveBatch(saveTagList);
        }
    }

    @Override
    //@CacheEvict(key = "'userId-tags:'+#userId",cacheNames = {"userTab"})
    public void addTags(List<String> tagCodeList) {
        //清除已选标签
        userTabDao.deleteByUserId(UserInfoUtil.getUserId());
        if(tagCodeList!=null && tagCodeList.size()>0){
            List<UserTabEntity> saveTagList = new ArrayList<>();
            tagCodeList.forEach(tag->{
                UserTabEntity userTab = new UserTabEntity() ;
                userTab.setCode(tag);
                userTab.setUserId(UserInfoUtil.getUserId());
                saveTagList.add(userTab) ;
            });
            saveBatch(saveTagList);
        }
    }

    @Override
    public List<String> getTagListByUserId(String userId) {
        List<String> tagList = new ArrayList<>();
        List<DictRes> dictList = dictService.listByParentCode(Constants.TAG_LIST);
        List<UserTabEntity> list = list(Wrappers.<UserTabEntity>lambdaQuery().eq(UserTabEntity::getUserId, userId));
        if(list!=null && list.size()>0){

            list.forEach(userTab->{
                dictList.forEach(dict->{
                    if(userTab.getCode().equals(dict.getCode())){
                        tagList.add(dict.getName());
                    }
                });
            });
        }
        return tagList;
    }


    @Override
    //@Cacheable(key = "'userId-tags:'+#userId",cacheNames = {"userTab"})
    public List<UserTagRes> getTagListBy(String userId) {
        return userTabDao.listByUserId(userId);
    }

    @Override
    public String getTagNameByCode(String parentCode,String code) {
        List<DictRes> dictList = dictService.listByParentCode(parentCode);
        if (CollectionUtils.isNotEmpty(dictList)) {
            return dictList.stream().filter(p->p.getCode().equals(code)).findFirst().orElse(new DictRes()).getName();
        }
        return null ;
    }

    @Override
    //@Cacheable(key = "'userId-tags:'+#userId",cacheNames = {"userTag"})
    public List<MyTagListRes> myList() {
        List<MyTagListRes> retList = new ArrayList<>();
        //获取所有标签字典
        List<UserTabEntity> list = userTabDao.selectList(Wrappers.<UserTabEntity>lambdaQuery().eq(UserTabEntity::getUserId, UserInfoUtil.getUserId()));

        List<DictEntity> dictList = dictService.cacheListByParentCode(Constants.TAG_LIST);
        //获取用户已选标签列表
        if(dictList!=null && dictList.size()>0){
            List<UserTabEntity> myTagList = userTabDao.selectList(Wrappers.<UserTabEntity>lambdaQuery().eq(UserTabEntity::getUserId, UserInfoUtil.getUserId()));
            dictList.forEach(dict->{
                MyTagListRes myTagListRes = new MyTagListRes();
                myTagListRes.setCode(dict.getCode());
                myTagListRes.setName(dict.getName());
                if (CollectionUtils.isNotEmpty(myTagList)){
                    myTagList.stream().filter(p->p.getCode().equals(dict.getCode())).findFirst().ifPresent(userTabEntity -> myTagListRes.setSelected(true));
                }
                retList.add(myTagListRes);
            }) ;
        }
        return retList;
    }
}